package com.zhengping.zpys.security.user;

import com.zhengping.zpys.base.AjaxResult;
import com.zhengping.zpys.security.role.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author chenweizuan
 * @description This is description of class
 * @version 1.0
 * @date 2020/3/28 18:36
 */
@RestController
@RequestMapping("/monitor/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/queryList")
    public AjaxResult queryList(@RequestBody(required = false) User user){
        List<User> users = userService.queryList(user);
        return AjaxResult.success(users);
    }

    @PostMapping("/creatUser")
    public AjaxResult creatUser(@RequestBody(required = false) User user){
        Integer row = userService.creatUser(user);
        if(row==0){
            AjaxResult.error("创建失败！");
        }
        return AjaxResult.success(row);
    }

    @PostMapping("/deleteList")
    public AjaxResult deleteList(@RequestBody(required = false) Long[] usersId){
        Integer row = userService.deleteList(usersId);

        if(row==0){
            AjaxResult.error("删除失败！");
        }
        return AjaxResult.success(row);
    }

    @GetMapping("/queryUser")
    public AjaxResult queryUser(@RequestParam(required = false) Long userId){
        User user = userService.queryUser(userId);
        return AjaxResult.success(user);
    }

    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody(required = false) User user){
        Integer row = userService.updateUser(user);
        if(row==0){
            AjaxResult.error("更新失败！");
        }
        return AjaxResult.success(row);
    }

    @PostMapping("/loginUser")
    public AjaxResult loginUser(@RequestParam(required = false) String userName, @RequestParam(required = false) String password){
        User user = userService.loginUser(userName, password);
        if(user==null){
            return AjaxResult.error("登录失败，账号或密码错误！");
        }
        return AjaxResult.success(user);
    }
}
